package oas.web;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 请求拦截器
 *
 * @author 2673
 * @version1.0
 */
@WebFilter("/LoginFilter")
public class RequestFilter implements Filter {


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

        ServletContext servletContext = filterConfig.getServletContext();
        // 获取XML文件中配置参数


    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
            throws IOException, ServletException {

        // 获得在下面代码中要用的request,response,session对象
        HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
        HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;

        // 获取请求URL
        String servletPath = httpRequest.getServletPath();


        // 检测获取的servletPath是否为不需要检测的URl中的一个.若是,放行
        if (!servletPath.endsWith("SelectBeer.jsp") && servletPath.endsWith(".jsp") && httpRequest.getMethod().equals("GET")) {
            httpResponse.sendRedirect(httpRequest.getContextPath() + "/SelectBeer.jsp");
            return;
        }

        filterChain.doFilter(httpRequest, httpResponse);
    }

    @Override
    public void destroy() {
    }

}
